package com.ascent.service;
import com.ascent.dao.UserMapper;
import com.ascent.classes.User;
import com.ascent.utils.DaoUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;


public class UserServiceImgl  {
    //1.创建SqlSessionFactory 工厂对象
    DaoUtil util = new DaoUtil();

    public User login(String username, String password) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        User user = mapper.login(username,password);
        //5. 释放资源
        sqlSession.close();
        return user;
    }

    public void add(User user) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取BrandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        mapper.add(user);
        sqlSession.commit();//提交事务
        //5. 释放资源
        sqlSession.close();
    }

    public int selectFlag(String username) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取BrandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        int flag = mapper.selectFlag(username);
        //5. 释放资源
        sqlSession.close();
        return flag;
    }
    public List<User> selectAll() throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        List<User> users = mapper.selectAll();
        //5. 释放资源
        sqlSession.close();
        return users;
    }
    /*
     *根据用户查所有
     * @Method selectByUser
     * @Date 2022/11/3 9:56
     */
    public User selectByUser(String username) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        User user = mapper.selectByUser(username);
        //5. 释放资源
        sqlSession.close();

        return user;
    }

   public User selectByUid(int uid) throws IOException {
       //2. 获取SqlSession对象
       SqlSession sqlSession = util.factory().openSession(true);//true自动提交
       //3. 获取UserMapper
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       //4. 调用方法
       User user = mapper.selectByUid(uid);
       //5. 释放资源
       sqlSession.close();

       return user;
    }

    /*
     *修改数据
     * @Method updateUserServlet
     * @Date 2022/11/3 9:56
     */
    public void updateUser(User user) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.调用方法
        mapper.updateUser(user);
        //6.释放资源
        sqlSession.close();
    }


    /*
     *根据用户查权限
     * @Method selectByUser
     * @Date 2022/11/3 9:56
     */
    public User selectSuperByUser(String username) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        User user = mapper.selectSuperByUser(username);
        //5. 释放资源
        sqlSession.close();
        return user;
    }

    /*
     *修改权限
     * @Method updateSuper
     * @Date 2022/11/3 13:30
     */
    public void updateSuper(String username,String superuser) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.调用方法
        mapper.updateSuper(username,superuser);
        //5.提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
    }

    /*
     *修改标志
     * @Method updateSuper
     * @Date 2022/11/3 13:30
     */
    public void updateFlag(String username, int del_flag) throws IOException {
        //2. 获取SqlSession对象
        SqlSession sqlSession = util.factory().openSession(true);//true自动提交
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.调用方法
        mapper.updateFlag(username,del_flag);
        //5.提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
    }

}
